package thread;
/**
 * 导致线程不安全
 *      抢占式执行,调度过程随机   --- 无能为力
 *      多个线程同时修改一个变量   --- 适当调整代码结构
 *      针对变量的操作,不是原子的  --- 加锁 synchronized
 *      内存可见性              ---  volatile
 *      指令重排序              --- synchronized
 */
public class TestDemo15 {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();

        Thread a = new Thread(() -> {
            synchronized (o1) {
                try {
                    o1.wait();
                    System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程a");
        Thread b = new Thread(() -> {
            synchronized (o2) {
                try {
                    o2.wait();
                    System.out.println(Thread.currentThread().getName());
                    synchronized (o1) {
                        o1.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程b");
        Thread c = new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
            synchronized (o2){
                o2.notify();
            }
        },"线程c");
        a.start();
        b.start();
        c.start();
    }
}
